<template>
  <div>
    <router-link to="/edit">
      <img src="../assets/docx.jpg"  alt="word" class="image">
    </router-link>
    <div style="padding: 20px;">
      <span style="display: inline-block; white-space: nowrap; width: 100%; overflow: hidden; text-overflow:ellipsis;">{{likeItem.name}}</span>
      <div class="bottom clearfix">
  
        <el-tooltip class="item" effect="dark" content="收藏" placement="bottom-start" v-if="!likeItem.is_starred">
          <el-button type="warning" plain icon="el-icon-star-off" circle @click="handleLike" size="mini"></el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="取消收藏" placement="bottom-start" v-else>
          <el-button type="warning" icon="el-icon-star-off" circle @click="handleLike" size="mini"></el-button>
        </el-tooltip>
      </div>
    </div>
  </div>
</template>

<script>
import {mapActions,mapState} from 'vuex'
import {reqLikeornotArticle} from '../api'
export default {
  name: "LikeArticle",
  props:{
    likeItem:Object,
  },
  data:function (){
    return{
      currentDate: new Date(),
    }
  },
  computed:{
    ...mapState(['userInfo','likeList'])
  },
  mounted () {
    this.recordLikeList(this.userInfo.userid)
  },
  methods:{
    ...mapActions(['getLikeList','getGarbageList','getCreateList','getCurrentList','recordLikeList']),
    handleLike(){
      this.likeItem.is_starred=!this.likeItem.is_starred
      if(this.likeItem.is_starred){//收藏
        //调用收藏接口，修改数据库收藏列表
        console.log(this.likeList.length)
        
        const result=reqLikeornotArticle(this.likeItem.id,true,this.userInfo.userid)
        //更新state中的收藏列表articleid,is_starred,userid
        console.log(result)

        setTimeout(() => {
          this.getLikeList()
          //更新回收站列表
        //  this.getGarbageList()
          //更新创建列表
          this.getCreateList()
          //更新浏览列表
        //  this.getCurrentList()
        },500)

        console.log(this.likeList.length)
      }
      else{//取消收藏
        //调用收藏接口，修改数据库收藏列表
        const result=reqLikeornotArticle(this.likeItem.id,false,this.userInfo.userid)
        //更新state中的收藏列表
        setTimeout(() => {
          this.getLikeList();
          //更新回收站列表
         // this.getGarbageList()
          //更新创建列表
          this.getCreateList()
          //更新浏览列表
        //  this.getCurrentList()
        },500)
        console.log(this.likeList.length)
      }
    }
  }
}
</script>

<style scoped>
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
  margin-top: 8px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>
